import { i18n } from 'boot/i18n'
import { QBtnToggle, QInput, QRange, QSlider, QToggle } from 'quasar'
import { isEmpty } from 'src/utils/utils'

/**
 * 获得表单字段对应组件
 */
export function getComponent (column = {}) {
  if (column.component) return column.component
  const { type } = column
  if (isEmpty(type)) return QInput
  if (type === 'select') return 'qx-select'
  if (['date', 'datetime', 'daterange'].includes(type)) return 'qx-date'
  if (['time'].includes(type)) return 'qx-time'
  if (type === 'number') return 'q-number'
  if (type === 'color') return 'qx-color'
  if (['radio', 'checkbox', 'toggle'].includes(type)) return 'q-group'
  if (['year', 'yearrange'].includes(type)) return 'q-year'
  if (['month', 'monthrange'].includes(type)) return 'q-month'
  if (type === 'tree') return 'q-select-tree'
  if (type === 'icon') return 'qx-icon'
  if (type === 'editor') return 'qx-editor'
  if (type === 'file') return 'qx-file'
  if (type === 'picture') return 'q-picture'
  if (type === 'switch') return QToggle
  if (type === 'btnToggle') return QBtnToggle
  if (type === 'range') return QRange
  if (type === 'slider') return QSlider
  return QInput
}

const regExp = /width.*?%;/g

/**
 * 获得表单通用属性
 */
export function getAttrs (column = {}, row, isSearch) {
  const { type = 'input', readonly, disable } = column, {
    // eslint-disable-next-line no-unused-vars
    rules, formslot, label, value, name, slot, component, lazyRules = true, required, hint, maxlength,
    ...params
  } = column
  const backupCol = { inline: true, outlined: true, lazyRules, noErrorIcon: true, autocomplete: 'off', ...params }
  if (typeof column.options === 'function') backupCol.options = column.options(row)
  backupCol.placeholder = getPlaceholder(column, isSearch)
  if (row) backupCol.rowInfo = row
  if (isSearch) backupCol['error-message'] = ' '
  if (type === 'switch') backupCol.size = 'xl'
  else if (type === 'textarea') backupCol.autogrow = true
  if (!readonly && !disable) {
    backupCol.hint = hint
    backupCol.rules = getRules(column)
    if (maxlength) {
      backupCol.counter = true
      backupCol.maxlength = maxlength
    }
  }
  if (component) backupCol.required = required
  if (backupCol.style) {
    const list = backupCol.style.split(';'), str = list.find(el => el.startsWith('width:'))
    if (str && str.endsWith('%')) backupCol.style = backupCol.style.replace(regExp, '')
  }
  return backupCol
}

/**
 * 获得表单字段验证规则
 */
export function getRules (column = {}) {
  const rules = column.rules || []
  if (column.required) rules.unshift(val => !isEmpty(val) || `${getPlaceholder(column)}`)
  return rules
}

const inputType = ['input', 'editor', 'number', 'textarea', 'password', '', undefined, null]

/**
 * 获得表单placeholder
 */
export function getPlaceholder (column = {}, isSearch = false) {
  if (column.placeholder) return column.placeholder
  if (isSearch) return `${column.label || ''}`
  if (!column.isSelectTips && inputType.includes(column.type)) return `${i18n.t('PleaseInput')}${column.label || ''}`
  return `${i18n.t('PleaseSelect')}${column.label || ''}`
}

export const formatColumn = (column = {}, isSearch) => {
  const {
    searchHint, searchTips, searchType, searchValue, searchRules, searchMultiple, searchRequired, searchReadonly,
    ...backupCol
  } = column
  if (isSearch) {
    searchType && (backupCol.type = searchType)
    delete backupCol.newValueMode
    delete backupCol['new-value-mode']
    backupCol.hint = searchHint
    backupCol.tips = searchTips
    backupCol.value = searchValue
    backupCol.rules = searchRules
    backupCol.multiple = searchMultiple
    backupCol.required = searchRequired
    backupCol.readonly = searchReadonly
  }
  delete backupCol.children
  return backupCol
}